package com.gaogzhen.datastructure.graph.undirected;

import edu.princeton.cs.algs4.StdIn;

/**
 * 无向图间隔度数
 * @author: Administrator
 * @createTime: 2023/03/14 21:17
 */
public class DegreesOfSeparation {
    public static void degreesOfSeparation(String filename, String delimiter, String source) {
        // 构建符号图
        SymbolGraph symbolGraph = new SymbolGraph(filename, delimiter);
        if (!symbolGraph.contains(source)) {
            System.out.println(source + " 不在数据库中");
            return;
        }
        // 符号图和起始顶点构建最短路径
        BreadthFirstPaths bfs = new BreadthFirstPaths(symbolGraph.graph(), symbolGraph.indexOf(source));

        while (!StdIn.isEmpty()) {
            String sink = StdIn.readLine();
            if (symbolGraph.contains(sink)) {
                int s = symbolGraph.indexOf(sink);
                if (bfs.hasPathTo(s)) {
                    for (Integer v : bfs.pathTo(s)) {
                        System.out.println("\t" + symbolGraph.nameOf(v));
                    }
                } else {
                    System.out.println(sink + " 与 " + source + " 不相连");
                }
            } else {
                System.out.println(sink + " 不在数据库中");
            }
        }
    }
}
